package org.ezplatform.office.meeting.info.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.ezplatform.core.dao.jpa.JpaBaseDao;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.office.meeting.info.dao.SignedInfoDao;
import org.ezplatform.office.meeting.info.entity.ApplyRecord;
import org.ezplatform.office.meeting.info.entity.SignedInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SignedInfoService extends BaseService<SignedInfo, String>{
    
    @Autowired
    private SignedInfoDao dao;
    
    @Autowired
    private ApplyRecordService applyRecordService;
    
    @Override
    protected JpaBaseDao<SignedInfo, String> getEntityDao() {
        return dao;
    }

    /**
     * 新建用户会议签到数据
     * @param entity
     */
    public void addSignedInfoByApplyrecord(ApplyRecord entity,String token) {
        List<String> userIdList = new ArrayList<String>();
        String corpId = entity.getCorpId();
        String applyRecordId = entity.getId();
        
        userIdList = applyRecordService.getApplyRecordUserList(entity,token);
        
        if(userIdList!=null && userIdList.size()>0){
            List<SignedInfo> list = new ArrayList<SignedInfo>();
            for(String userId : userIdList){
                SignedInfo s = new SignedInfo();
                s.setApplyRecordId(applyRecordId);
                s.setSignedUserId(userId);
                s.setIsSigned(null);
                s.setCorpId(corpId);
                list.add(s);
            }
            this.save(list);
        }
    }

    /**
     * 新建用户会议签到数据
     * @param userIdList
     * @param applyRecordId
     * @param corpId
     */
    public void addSignedInfo(List<String> userIdList, String applyRecordId, String corpId) {
        if(userIdList!=null && userIdList.size()>0){
            List<SignedInfo> list = new ArrayList<SignedInfo>();
            for(String userId : userIdList){
                SignedInfo s = new SignedInfo();
                s.setApplyRecordId(applyRecordId);
                s.setSignedUserId(userId);
                s.setIsSigned(null);
                s.setCorpId(corpId);
                list.add(s);
            }
            this.save(list);
        }
    }
    
    /**
     * 新建用户会议签到数据(周期会议)
     * @param userIdList
     * @param applyRecordId
     * @param corpId
     */
    public void addCycleSignedInfo(List<ApplyRecord> arCycleList, List<String> userIdList, String applyRecordId, String corpId) {
    	if(CollectionUtils.isNotEmpty(arCycleList)){
            for(ApplyRecord ar : arCycleList) {
            	if(!applyRecordId.equals(ar.getId()))
            		addSignedInfo(userIdList, ar.getId(), corpId);
            }
        }
    }
    
    /**
     * 查询签到参会人员
     * @param applayRecord 会议id
     * @param type 0:未签到 1：已签到
     * @return
     */
    public String getSignedUserName(String applayRecord, int type) {
        String userName = "";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        StringBuffer sql = new StringBuffer("");
        sql.append(" select omsi.signed_user_id userId,sysu.user_name userName from oa_meeting_signed_info omsi");
        sql.append(" left join sys_user sysu on omsi.signed_user_id = sysu.id ");
        sql.append(" left join oa_meeting_apply_record omar on omsi.apply_record_id = omar.id");
        sql.append(" where omsi.apply_record_id =:applayRecord");
        paramMap.put("applayRecord", applayRecord);
        if(type==0){
    		sql.append(" and ((omar.signed_type='0' and omsi.is_signed!='s') ");
    		sql.append(" or (omar.signed_type='1' and omsi.is_signed!='d') ");
    		sql.append(" or (omar.signed_type='2' and omsi.is_signed!='sd') ");
    		sql.append(" or (omar.signed_type='3' and omsi.is_signed!='xc') or omsi.is_signed ='0' or omsi.is_signed is null) ");
        }else if(type==1){
        	sql.append(" and ((omar.signed_type='0' and omsi.is_signed='s') ");
    		sql.append(" or (omar.signed_type='1' and omsi.is_signed='d') ");
    		sql.append(" or (omar.signed_type='2' and omsi.is_signed='sd') ");
    		sql.append(" or (omar.signed_type='3' and omsi.is_signed='xc') or omsi.is_signed ='0') ");
        }
        sql.append(" order by sysu.user_name asc");
        
        List list = super.getQueryByNativeSQL(sql.toString(), paramMap).getResultList();
        if(list!=null && list.size()>0){
            for(int i=0;i<list.size();i++){
                Object[] obj = (Object[]) list.get(i); 
                userName += obj[1]+",";
            }
        }
        if(StringUtils.isNotEmpty(userName)){
        	userName = userName.substring(0, userName.length()-1);
        }
        return userName;
    }
    
    /**
     * 根据会议id删除签到信息
     * @param recordId
     * @return
     */
    public int deleteSignInfo(String... recordId){
    	return this.dao.delByRecordId(recordId);
    }
    
    /**
     * 查询会议签到
     * @param noticeUserId
     * @param applyRecordId
     * @return
     */
    public SignedInfo getSignedInfo(String userId, String applyRecordId) {
        List<SignedInfo> list = this.dao.getSignedInfoList(userId,applyRecordId);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
    
    /**
     * 保存(保留方法)
     * @param id
     */
    public SignedInfo editSignedInfo(String id) {
        SignedInfo signedInfo = this.findOne(id);
        signedInfo.setIsSigned("");
        return this.save(signedInfo);
    }
    
    /**
     * 查询签到参会人员
     * @param applayRecord 会议id
     * @param type 0:未签到 1：已签到
     * @return
     */
    public List getSignedUserList(String applayRecord, int type) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        StringBuffer sql = new StringBuffer("");
        sql.append(" select omsi.signed_user_id userId,sysu.user_name userName,sysu.outside_link,sysu.dd_uid from oa_meeting_signed_info omsi");
        sql.append(" left join sys_user sysu on omsi.signed_user_id = sysu.id ");
        sql.append(" left join oa_meeting_apply_record omar on omsi.apply_record_id = omar.id");
        sql.append(" where omsi.apply_record_id =:applayRecord");
        paramMap.put("applayRecord", applayRecord);
        if(type==0){
    		sql.append(" and ((omar.signed_type='0' and omsi.is_signed!='s') ");
    		sql.append(" or (omar.signed_type='1' and omsi.is_signed!='d') ");
    		sql.append(" or (omar.signed_type='2' and omsi.is_signed!='sd') ");
    		sql.append(" or (omar.signed_type='3' and omsi.is_signed!='xc')  or omsi.is_signed ='0' or omsi.is_signed is null) ");
        }else if(type==1){
        	sql.append(" and ((omar.signed_type='0' and omsi.is_signed='s') ");
    		sql.append(" or (omar.signed_type='1' and omsi.is_signed='d') ");
    		sql.append(" or (omar.signed_type='2' and omsi.is_signed='sd') ");
    		sql.append(" or (omar.signed_type='3' and omsi.is_signed='xc') or omsi.is_signed ='0') ");
        }
        sql.append(" order by sysu.user_name asc");
        
        List list = super.getQueryByNativeSQL(sql.toString(), paramMap).getResultList();
        return list;
    }
    
    public void deleteSignedInfo(List<String> signedInfoUserIdList, String applyRecordId, String corpId) {
    	dao.deleteSignedInfo(signedInfoUserIdList, applyRecordId, corpId);
    }
    
    public void deleteSignedInfoByApplyrecordId(String applyRecordId, String corpId) {
    	dao.deleteSignedInfoByApplyrecordId(applyRecordId, corpId);
    }
}